package com.yanqu.road.server.logic.activity.condition.coupon;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.ePlayerAttrType;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.AttributeModule;
import com.yanqu.road.server.gameplayer.module.player.CouponModule;
import com.yanqu.road.server.gameplayer.module.player.PotentialModule;
import com.yanqu.road.server.gameplayer.module.task.TaskModule;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.logic.activity.condition.BaseActivityCondition;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.utils.date.DateHelper;

import java.math.BigInteger;

public class CouponTriggerActivityCondition extends BaseActivityCondition {

    public CouponTriggerActivityCondition(ActivityInfo activityInfo, ActivityConditionInfo activityConditionInfo, BaseActivityData baseActivityData) {
        super(activityInfo, activityConditionInfo, baseActivityData);
    }

    @Override
    public void addTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().addListener(eGamePlayerEventType.CouponTrigger.getValue(), handle);
    }

    @Override
    public void removeTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().removeListener(eGamePlayerEventType.CouponTrigger.getValue(), handle);
    }

    @Override
    public boolean isCompleted(GamePlayer player) {
        if(getValue().compareTo(BigInteger.ONE) == 0){
            return true;
        }
        return false;
    }

    @Override
    protected void player_BoardLevelHandle(Object object) {
        CouponNotifyArgs arg = (CouponNotifyArgs) object;
        if (getValue().compareTo(BigInteger.ONE) == 0) {//已经触发过了
            return;
        }

        int param1 = activityConditionInfo.getParamList().get(0).intValue();        //开服天数条件
        int param3 = activityConditionInfo.getParamList().get(2).intValue();        //潜力分下限
        int param4 = activityConditionInfo.getParamList().get(3).intValue();        //潜力分上限
        int param5 = activityConditionInfo.getParamList().get(4).intValue();        //1：直接触发，0：下次登录触发
        int param6 = activityConditionInfo.getParamList().get(5).intValue();        //身份条件
        int param7 = activityConditionInfo.getParamList().get(6).intValue();        //充值次数
        int param8 = activityConditionInfo.getParamList().get(7).intValue();        //付费过，最近未付费天数
        int param9 = 0;
        if (activityConditionInfo.getParamList().size() > 8) {
            param9 = activityConditionInfo.getParamList().get(8).intValue();        //累计vip经验
        }
        int param10 = 0;
        if (activityConditionInfo.getParamList().size() > 9) {
            param10 = activityConditionInfo.getParamList().get(9).intValue();        //累计使用代金券次数
        }

        //下次登录触发的
        if (arg.isLogin() && param5 == 0 && getPreValue().compareTo(BigInteger.ONE) == 0) {//触发
            setValue(BigInteger.ONE);
            return;
        }

        if (param5 == 0 && getPreValue().equals(BigInteger.ONE)) {//已经触发过了
            return;
        }

        int openServerDay = ServerListMgr.getServerOpenDay(GameServer.getInstance().getServerId());
        int title = player.getTitleId();
        int potential = player.getModule(PotentialModule.class).getPotential();
        AttributeModule attributeModule = player.getModule(AttributeModule.class);
        long rechargeTimes = attributeModule.getAttribute(ePlayerAttrType.RechargeTotalTimes);
        long lastRechargeTime = attributeModule.getAttribute(ePlayerAttrType.LastRechargeTime);

        if(openServerDay < param1){//开服天数
            return;
        }
        if (title < param6) {//身份条件
            return;
        }
        if (potential < param3 || potential > param4) {//潜力分
            return;
        }
        if (rechargeTimes < param7) {
            return;
        }
        if (param8 > 0) {
            if (rechargeTimes < 0) {
                return;
            }
            long nowTime = System.currentTimeMillis() / 1000;
            if ((lastRechargeTime + DateHelper.DAY_SECONDS * param8) >= nowTime) {//n天内有充值
                return;
            }
        }
        if (param9 > 0) {
            long vipExp = player.getUserInfo().getVipExp();
            if (vipExp < param9) {
                return;
            }
        }
        if (param10 > 0) {
            int couponUsedCount = player.getModule(CouponModule.class).getCouponUsedCount();
            if (couponUsedCount < param10) {
                return;
            }
        }

        if (1 == param5) {//直接触发
            setValue(BigInteger.ONE);
        } else {//下次登录触发
            setPreValue(BigInteger.ONE);
        }
    }
}
